/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.models;

import java.io.StringReader;
import org.xml.sax.InputSource;
import org.zoundry.gaedt.core.models.fixtures.Child1Model;
import org.zoundry.gaedt.core.models.fixtures.RootModel;
import test.org.zoundry.gaedt.core.ZGaeCoreTestCase;


/**
 * Tests the generic sax model reader.
 */
public class ZGaeSaxModelReaderTest extends ZGaeCoreTestCase
{
    /**
     * A very simple model reader test.
     *
     * @throws Exception
     */
    public void testSimpleModel() throws Exception
    {
        String xml = "<root name=\'MyRoot\' xmlns=\'urn:test\' xmlns:ns1=\'urn:ns1\'>\r\n" +
        "   <child1 xmlns:ns1=\'urn:ns2\'>ns1:Child1Value</child1>\r\n" +
        "</root>";
        ZGaeModelFactory modelFactory = new ZGaeModelFactory("", "Model");
        modelFactory.setModelClassLoader(ZGaeSaxModelReaderTest.class.getClassLoader());
        modelFactory.setDefaultPackage("org.zoundry.gaedt.core.models.fixtures");
        IZGaeModelTypeMapper typeMapper = new ZGaeModelTypeMapper();
        IZGaeModelMutatorProvider mutatorProvider = new ZGaeModelMutatorProvider();
        ZGaeSaxModelReader reader = new ZGaeSaxModelReader(modelFactory, typeMapper, mutatorProvider);
        ZGaeModel model = reader.read(new InputSource(new StringReader(xml)));
        assertNotNull("The model was null but should not have been.", model);
        assertTrue("The model should have been a RootModel but was a " + model.getClass().getName(), model instanceof RootModel);
        RootModel rootModel = (RootModel) model;
        assertEquals("MyRoot", rootModel.getName());
        Child1Model childModel = rootModel.getChild1();
        assertNotNull("Child model was null but should not have been.", childModel);
        assertEquals("ns1:Child1Value", childModel.getValue());
    }
}
